import Vue from 'vue';
const self=new Vue();
const groupKeyPrefix="group_";
//将群组列表存入本地
function setMyGroupListToLocal(userId,groupList,reloadFlag){
	let key=groupKeyPrefix+userId;
	if(groupList&&groupList.length>0){
		console.log("=========更新本地群组记录====");
		groupList.sort(function(a,b){
			return  b.groupUser.topFlag-a.groupUser.topFlag
		})
		uni.removeStorageSync(key);
		let groupListStr= encodeURIComponent(JSON.stringify(groupList));
		uni.setStorageSync(key,groupListStr);
		if(reloadFlag==true){
			uni.$emit("groupListener",null);
		}
	}
}
//添加新的群组到群组列表
function addNewGroupToLocal(that,newChatGroup){
	 let userId=that.vuex_userInfo.id;
	 let key=groupKeyPrefix+userId;
	 let myGroupListStr= uni.getStorageSync(key);
	 let myGroupList=[];
	 if(myGroupListStr!=null&&myGroupListStr!=undefined&&myGroupListStr.length>0){
	 	myGroupList=JSON.parse(decodeURIComponent(myGroupListStr));
	 	myGroupList.unshift(newChatGroup);
	 }
	 else{
	 	myGroupList.push(newChatGroup);
	 }
	 setMyGroupListToLocal(userId,myGroupList,true);
}
//从群组列表删除某个群组并更新本地记录
function removeGroupFromLocal(groupId,that){
	 let userId=that.vuex_userInfo.id;
	 let key=groupKeyPrefix+userId;
	 let myNewGroupList=[];
	 let myGroupListStr= uni.getStorageSync(key);
	 if(myGroupListStr!=null&&myGroupListStr!=undefined&&myGroupListStr.length>0){
		let myOldGroupList=JSON.parse(decodeURIComponent(myGroupListStr));
		myNewGroupList=myOldGroupList.filter((element)=>{
			return element.imGroup.groupId != groupId
		})
		setMyGroupListToLocal(userId,myNewGroupList,false);
	 }
	 return myNewGroupList;
}
//更新群聊列表记录
function updateGroupFromLocal(group,that){
	 let userId=that.vuex_userInfo.id;
	 let key=groupKeyPrefix+userId;
	 let myGroupListStr= uni.getStorageSync(key);
	 if(myGroupListStr!=null&&myGroupListStr!=undefined&&myGroupListStr.length>0){
		let myOldGroupList=JSON.parse(decodeURIComponent(myGroupListStr));
		let newGroupList= myOldGroupList.map((element)=>{
			if(element.imGroup.groupId == group.imGroup.groupId){
				element=group;
			}
			return element;
		});
		setMyGroupListToLocal(userId,newGroupList,true);
	 }
}
//更新我的群聊设置
function updateMyGroupSetInfo(group,that,callback){
	let param=group.groupUser;
	console.log("更新我的群聊设置参数",param);
	that.$u.api.chatGroup.updateMyGroupSet(param).then(res => {
		console.log("更新我的群聊设置结果",res);
		uni.$emit("updateGroupList",null);//刷新列表
		callback(res);
	})
}
//更新服务端群聊信息
function updateGroupInfoFromServer(that,baseGroupInfo,callback){
	that.$u.api.chatGroup.updateGroupInfo(baseGroupInfo).then(res=>{
		console.log("修改群信息结果",res);
		if(res.code==200){
			callback(true);
		}else{
			console.log("修改群信息结果失败");
			callback(false)
		}
	}).catch(res => {
		console.log("修改群信息结果异常",res);
		callback(false)
	})
}
//更新本地群组列表基础信息(群名称,群屏蔽,群置顶)
function updateLocalGroupInfo(that,baseGroupInfo){
	let userId=that.vuex_userInfo.id;
	let key=groupKeyPrefix+userId;
	let myGroupListStr= uni.getStorageSync(key);
	if(myGroupListStr!=null&&myGroupListStr!=undefined&&myGroupListStr.length>0){
		let myOldGroupList=JSON.parse(decodeURIComponent(myGroupListStr));
		let newGroupList= myOldGroupList.map((element)=>{
			if(element.imGroup.groupId == baseGroupInfo.groupId){
				element.imGroup=Object.assign(element.imGroup,baseGroupInfo);
			}
			return element;
		});
		console.log("更新群组信息",newGroupList.length);
		setMyGroupListToLocal(userId,newGroupList,true);
	}
}
//获取我的群组列表
function  getMyGroupList(that,callback){
	let userId=that.vuex_userInfo.id;
	let key=groupKeyPrefix+userId;
	let myGroupList=[];
	//需要从服务端获取数据 reloadMessageFlag是存储在vuex变量的标志,判断是否需要从服务端加载数据
	if (that.reloadMessageFlag == 1) {
		loadGroupListFromServer(that,function(myGroupList){
			console.log("---------从[***服务端***]加载群组列表------------",myGroupList);
			setMyGroupListToLocal(userId,myGroupList,false);
			callback(myGroupList);
		});
	}else{
		let myGroupListStr= uni.getStorageSync(key);
		//加载本地的
		if(myGroupListStr!=null&&myGroupListStr!=undefined&&myGroupListStr.length>0){
			 myGroupList=JSON.parse(decodeURIComponent(myGroupListStr));
			 console.log("从[****本地****]获取群组列表记录",myGroupList);
			 callback(myGroupList);
		}/* else{
			myGroupList=loadGroupListFromServer(that);
		} */
	}
}
//从服务端加载我的群组列表并且从服务端循环更新每个群组的信息记录
//这里一般是用户主动退出登录时需要进行的数据初始化加载
 function  loadGroupListFromServer(that,callback){
	 let totalUnreadNum=0;
	 that.$u.api.chatGroup.getMyChatGroupList({}).then(res => {
		if (res.code == 200) {
			let data = res.data;
			if (data.length > 0) {
				for (var i = 0; i < data.length; i++) {
					let groupData = data[i];
					let groupId =groupData.imGroup.groupId;
					let groupType=groupData.imGroup.groupType;
					if(groupType==0){
						let unreadNum=groupData.groupUser.unReadNum;
						totalUnreadNum=totalUnreadNum+unreadNum;
					}
					that.messageApi.loadGroupMessageFromServer(that,groupId,1,200);
				};
				that.localGroupApi.setTabBarBadge(0,totalUnreadNum);
				that.$u.vuex("reloadMessageFlag", 0);
				callback(data)
			} else {
				callback([])
			}
			that.$u.vuex("reloadMessageFlag", 0);
		} else {
			callback([])
		}
	}).catch(res => {
		console.log("服务端加载群组列表数据异常",res);
		callback([])
		that.globalUtil.utilAlert("服务端加载数据异常!");
		that.$u.vuex("reloadMessageFlag", 1);
	})
}

//从服务端加载群组详情
function loadNewGroupInfoFromServer(that,groupId){
	let param = {
		chatGroupId:groupId
	};
	that.$u.api.chatGroup.getChatGroupInfo(param).then(res => {
		if (res.code == 200) {
			let data = res.data;
			console.log("新的群信息",data);
			addNewGroupToLocal(that,data);
		} else {
			return;
		}
	});
}

function setTabBarBadge(tabbarIndex,text){
	if(text==0){
		removeTabBarBadge(tabbarIndex);
		return;
	}
	uni.setTabBarBadge({
	  index: tabbarIndex,
	  text: text+''
	})
}

function showTabBarRedDot(tabbarIndex){
	uni.removeTabBarBadge({
		index: tabbarIndex,
	})
}

function removeTabBarBadge(tabbarIndex){
	uni.removeTabBarBadge({
		index: tabbarIndex,
	})
}

module.exports ={
	setMyGroupListToLocal:function(userId,groupList,reloadFlag){
		return setMyGroupListToLocal(userId,groupList,reloadFlag);
	},
	addNewGroupToLocal:function(that,newChatGroup){
		return addNewGroupToLocal(that,newChatGroup);
	},
	getMyGroupList:function(that,callback){
		return  getMyGroupList(that,callback);
	},
	loadNewGroupInfoFromServer:function(that,groupId){
		return loadNewGroupInfoFromServer(that,groupId);
	},
	setTabBarBadge,
	showTabBarRedDot,
	removeTabBarBadge,
	updateGroupFromLocal,
	updateMyGroupSetInfo,
	updateGroupInfoFromServer,
	updateLocalGroupInfo,
	removeGroupFromLocal
}
